package com.neu.af.dataFilter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.af.dataFilter.entity.Rule;
import com.neu.af.dataFilter.entity.Tag;
import com.neu.af.dataFilter.entity.TagRuleAlarm;
import com.neu.af.dataFilter.mapper.RuleMapper;
import com.neu.af.dataFilter.mapper.TagRuleAlarmMapper;
import com.neu.af.dataFilter.service.RuleService;
import com.neu.af.dataFilter.service.TagRuleAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ruleService")
public class RuleServiceImpl implements RuleService {
    @Autowired
    RuleMapper ruleMapper;
    @Autowired
    TagRuleAlarmMapper tagRuleAlarmMapper;
    @Autowired
    TagRuleAlarmService tagRuleAlarmService;

    @Override
    public int insert(Rule rule) {
        List<TagRuleAlarm> nullTagRuleAlarms = tagRuleAlarmMapper.getNullTagRuleAlarms();
        // 对原先没有规则匹配的tagRuleAlarm进行匹配
        if (nullTagRuleAlarms!=null || nullTagRuleAlarms.size()!=0) {
            for (TagRuleAlarm tra: nullTagRuleAlarms) {
                if(tra.getDescriptor().contains(rule.getContains())) {
                    tra.setContains(rule.getContains());
                    tra.setAlarm_class(rule.getAlarm_class());
                    tra.setRule_value("-1");
                }
                tagRuleAlarmMapper.update(tra);
            }
        }
        if (rule.getAlarm_class()==1) {
            List<TagRuleAlarm> tagRuleAlarmsTwo = tagRuleAlarmMapper.getByAlarmClass(2);

            for (TagRuleAlarm t: tagRuleAlarmsTwo) {
                if(t.getDescriptor().contains(rule.getContains())) {
                    t.setContains(rule.getContains());
                    t.setAlarm_class(1);
                    tagRuleAlarmMapper.update(t);
                }
            }
        }
        return ruleMapper.insert(rule);
    }

    @Override
    public int update(Rule rule) {
        Rule rule0 = ruleMapper.getById(rule.getId()); // 原来的rule
        List<TagRuleAlarm> tagRuleAlarms = tagRuleAlarmMapper.getByContains(rule.getContains());
        // 只改了等级
        if(rule.getContains().equals(rule0.getContains()) && rule.getAlarm_class() != rule0.getAlarm_class()) {
            for (TagRuleAlarm tra: tagRuleAlarms) {
                tra.setAlarm_class(rule.getAlarm_class());
                tagRuleAlarmMapper.update(tra);
            }
        }
        // 两者都改了
        else if (!rule.getContains().equals(rule0.getContains())) {
            delete(rule.getId());
            insert(rule);
        }

        return ruleMapper.update(rule);
    }

    @Override
    public int delete(String id) {
        Rule r = ruleMapper.getById(id);
        List<TagRuleAlarm> tagRuleAlarms = tagRuleAlarmMapper.getByContains(r.getContains());
        ruleMapper.delete(id);
        List<Rule> rules_one = ruleMapper.getByAlarmClass("1");
        List<Rule> rules_two = ruleMapper.getByAlarmClass("2");
        for (TagRuleAlarm tra:tagRuleAlarms) {
            tagRuleAlarmMapper.delete(tra.getId());
            Tag t = new Tag();
            t.setDescriptor(tra.getDescriptor());
            t.setTag_name(tra.getTag_name());
            tagRuleAlarmService.insertOne(t, rules_one, rules_two);
        }
        return 1;
    }

    @Override
    public Rule getById(String id) {
        return ruleMapper.getById(id);
    }

    @Override
    public List<Rule> getAll() {
        return ruleMapper.getAll();
    }

    @Override
    public List<Rule> getAllByFilter(Map<String, Object> map) {
        return ruleMapper.getAllByFilter(map);
    }

    @Override
    public int deleteByContains(String contains) {
        return ruleMapper.deleteByContains(contains);
    }

    @Override
    public Rule getByContains(String contains) {
        return ruleMapper.getByContains(contains);
    }

    @Override
    public List<Rule> getByContainsLike(String contains) {
        return ruleMapper.getByContainsLike(contains);
    }

    @Override
    public PageInfo<Rule> getAllByFilter(Integer pageNum, Integer pageSize) {
        return this.getAllByFilter(pageNum, pageSize, new HashMap());
    }

    @Override
    public PageInfo<Rule> getAllByFilter(Integer pageNum, Integer pageSize, Map<String, Object> map) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Rule> rules = ruleMapper.getAllByFilter(map);
        return new PageInfo<>(rules);
    }


}
